import { loginApi } from '@/api/login'
import router from '@/router'
import { setTokenTime } from '@/utils/auth'
import { ElMessage } from 'element-plus'
export default {
  namespaced: true,
  state: () => ({
    token: localStorage.getItem('token') || '',
    siderType: true,
    lang: localStorage.getItem('lang') || 'zh'
  }),
  mutations: {
    setToken(state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },
    changeSiderType(state) {
      state.siderType = !state.siderType
    },
    changLang(state, lang) {
      state.lang = lang
    }
  },
  actions: {
    async login({ commit }, info) {
      const result = await loginApi({
        name: info.username,
        password: info.password
      })
      if (!result.token) {
        return ElMessage.error(result.msg)
      }
      ElMessage({
        message: '登录成功！',
        type: 'success'
      })
      commit('setToken', result.token)
      router.replace('/article')
    },
    // 退出
    logout({ commit }) {
      commit('setToken', '')
      localStorage.clear()
      router.replace('/login')
    }
  }
}
